package com.reran.homework;

import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: WangSiTeng
 * @date: 2021/11/28
 */
public class AsyncRun02 {


    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();

        SumThread sumThread = new SumThread();
        sumThread.start();
        //这是得到的返回值
        while (!sumThread.isSuccess()) {
            TimeUnit.MILLISECONDS.sleep(1);
        }
        int result = sumThread.getResult();
        // 确保  拿到result 并输出
        System.out.println("异步计算结果为：" + result);
        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");
    }

    private static int sum() {
        return fibo(36);
    }

    private static int fibo(int a) {
        if (a < 2) {
            return 1;
        }
        return fibo(a - 1) + fibo(a - 2);
    }

    static class SumThread extends Thread {
        private Integer result;
        private volatile boolean success = false;

        public Integer getResult() {
            return result;
        }

        public boolean isSuccess() {
            return success;
        }

        @Override
        public void run() {

            result = sum();
            success = true;
        }

    }
}
